import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/pages/home/info/info_item.dart';
import 'package:flutter_app/routes/routes.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class HoseInfomation extends StatefulWidget {
  const HoseInfomation({Key? key}) : super(key: key);

  @override
  _HoseInfomationState createState() => _HoseInfomationState();
}

List _list = [
  {
    "imagsrc": 'images/banner.png',
    "title": "111111111111111111111",
    "from": '网易新闻',
    "time": '2021',
    "pushUrl": '/login'
  },
  {
    "imagsrc": 'images/banner.png',
    "title": "2222",
    "from": '网易新闻',
    "time": '2022',
    "pushUrl": '/login'
  },
];

class _HoseInfomationState extends State<HoseInfomation> {
  late List _infoList = [];
  _getInfoLidst() async {
    var res = await DioHttp.of(context).get('/home/news');
    var resString = json.decode(res.toString());
    var status = resString['status'];
    String desc = resString["description"] ?? "内部错误";
    if (status.toString().startsWith('2')) {
      var infoList = resString["body"];
      setState(() {
        _infoList = infoList;
      });
    } else {
      EasyLoading.showError(desc);
    }
  }

  @override
  void initState() {
    _getInfoLidst();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(top: 10, left: 10),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Padding(
                padding: EdgeInsets.only(left: 10),
                child: Text(
                  '最新资讯',
                  style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                ),
              ),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pushNamed(Routes.latestInfo);
                },
                child: const Padding(
                  padding: EdgeInsets.only(right: 20),
                  child: Text(
                    '更多',
                    style: TextStyle(color: Colors.grey),
                  ),
                ),
              ),
            ],
          ),
          Column(
            children: _infoList.map((e) => InfoItem(data: e)).toList(),
          )
        ],
      ),
    );
  }
}
